package insight.google.game.era.servlet;

import insight.google.game.era.data.GameResult;
import insight.google.game.era.data.Host;
import insight.google.game.era.data.Player;
import insight.google.game.era.data.Participant;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
              throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        String userId = null;
        if (user != null) {
        	//MOdified by Monirul
//        	req.getParameter("userName");
        	 String logoutURL = userService.createLogoutURL("http://www.spectrum.co.jp");
             resp.sendRedirect("../dashboard.html");
            // Modified by Monirul 
        	userId = user.getUserId();
        	PrintWriter writer = resp.getWriter();
            resp.setContentType("text/html");
            
            writer.println("Hello, " + user.getNickname());
            writer.println("USER Information is as below."
            		+ "<br>Email: " + user.getEmail()
            		+ "<br>Nickname: " + user.getNickname()
            		+ "<br>Auth Domain: " + user.getAuthDomain()
            		+ "<br>UserID" + userId);
           
            ServletContext context = getServletContext();
            Host host = (Host) getServletContext().getAttribute(Host.class.getName());
            if (host == null) {
            	host = new Host();
            	context.setAttribute(Host.class.getName(), host);
            }
            Map<String, Player> userMap = (Map<String, Player>) context.getAttribute(Player.class.getName());
            Player gameUser = null;
            if (userMap != null) {
            	gameUser = userMap.get(userId);
            	if (gameUser == null) {
            		gameUser = new Player(userId, user.getNickname(), "");
            		userMap.put(userId, gameUser);
            	}
            	String numberParam = req.getParameter("number");
            	if ((numberParam != null) && numberParam.equals(req.getSession().getAttribute("number"))) {
            		gameUser.setWinTime(gameUser.getWinTime() + 1);
            	}
            	gameUser.setGameTime(gameUser.getGameTime() + 1);
        		
        		String url = req.getRequestURI();
        		int offset = url.indexOf("?");
        		url = (offset == -1) ? url : url.substring(0, offset);
        		writer.println("<p><center>Please click one link below.<br>");
        		writer.println("<A href = '" + (url + "?number=0'>Click Me!</A>"));
        		writer.println("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A href = '" + (url + "?number=1'>No, CLick Me!</A>"));
        		
        		Iterator<Player> iterator = userMap.values().iterator();
        		writer.println("<br><table>");
        		while (iterator.hasNext()) {
        			writer.println("<tr>");
        			writer.println("<td>" + iterator.next().toString() + "</td>");
        			writer.println("</tr>");
        		}
        		writer.println("</table><br>");
        		long number = System.currentTimeMillis() % 2;
        		req.getSession().setAttribute("number", String.valueOf(number));
            } else {
            	gameUser = new Player(user.getUserId(), user.getNickname(), "");
        		userMap = new HashMap<String, Player>();
        		userMap.put(userId, gameUser);
        		context.setAttribute(Player.class.getName(), userMap);
            }

            if (host.join(gameUser)) {
            	writer.println("<p>&nbsp;</p><center><A href='/GameCenter.jsp'>You can join Game Center.</A></center>");
            } else {
            	writer.println("<p>&nbsp;</p><center><A href='/GameCenter.jsp'>You already joined Game Center.</A></center>");
            }
            
            writer.println("<br><br><br>you can logout by this link: <A href='" + logoutURL + "'>Logout</A>");
        } else {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        }
    }
}